﻿using SharedModule;
using System.Text.Json;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EventBus.Distributed;

namespace 接收日志
{
    public class EventMessagingService : ITransientDependency
    {
        private readonly IDistributedEventBus _distributedEventBus;

        public EventMessagingService(IDistributedEventBus distributedEventBus)
        {
            _distributedEventBus = distributedEventBus;
        }

        public async Task RunAsync()
        {
            Console.WriteLine("*** 接收自己发送日志项目启动 ***");
            Console.WriteLine("*** 选择下面操作： ***");
            Console.WriteLine("*** 1: 发布一条日志事件 ***");

            string message;
            do
            {
                Console.WriteLine();

                message = Console.ReadLine();

                if (message.Equals("1"))
                {
                    //待发送消息
                    EventCreatePostDto eventCreatePostDto = new EventCreatePostDto()
                    {
                        StationType = StationTypeEnum.SttSmsc,
                        UserId = 2,
                        EventLevel = EventLevelEnum.Info,
                        EventSubType = EventSubTypeEnum.Login,
                        DesTemplateId = 137740943366,
                        CreateTime = DateTime.UtcNow,
                        Des = JsonSerializer.Serialize(
                                    new EventKeyInfoDto
                                    {
                                        KeyInfos = new List<KeyInfo>
                                        {
                                            new KeyInfo
                                            {
                                                Index = 0,
                                                NeedTranslation = false,
                                                Params = "s1",
                                            }
                                        }
                                    }),
                    };

                    await _distributedEventBus.PublishAsync(new EventLogData(eventCreatePostDto));
                    await Console.Out.WriteLineAsync("发送成功！");
                }
            } while (!message.IsNullOrEmpty());
        }
    }
}